<?php

/**
 * This is the model class for table "custom_fields".
 *
 * The followings are the available columns in table 'custom_fields':
 * @property integer $id
 * @property string $type
 * @property string $name
 * @property string $field_format
 * @property string $possible_values
 * @property string $regexp
 * @property integer $min_length
 * @property integer $max_length
 * @property integer $is_required
 * @property integer $is_for_all
 * @property integer $is_filter
 * @property integer $position
 * @property integer $is_searchable
 * @property string $default_value
 * @property integer $is_editable
 * @property integer $is_visible
 *
 * The followings are the available model relations:
 * @property CustomFieldsProjects[] $customFieldsProjects
 * @property CustomFieldsTrackers[] $customFieldsTrackers
 * @property CustomValues[] $customValues
 */
class CustomField extends CActiveRecord
{
    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return CustomFields the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'custom_fields';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('min_length, max_length, is_required, is_for_all, is_filter, position, is_searchable, is_editable, is_visible', 'numerical', 'integerOnly'=>true),
            array('type, name, field_format', 'length', 'max'=>30),
            array('regexp', 'length', 'max'=>255),
            array('possible_values, default_value', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, type, name, field_format, possible_values, regexp, min_length, max_length, is_required, is_for_all, is_filter, position, is_searchable, default_value, is_editable, is_visible', 'safe', 'on'=>'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'customFieldsProjects' => array(self::HAS_MANY, 'CustomFieldsProject', 'custom_field_id'),
            'customFieldsTrackers' => array(self::HAS_MANY, 'CustomFieldsTracker', 'custom_field_id'),
            'customValues' => array(self::HAS_MANY, 'CustomValue', 'custom_field_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'type' => 'Type',
            'name' => 'Name',
            'field_format' => 'Field Format',
            'possible_values' => 'Possible Values',
            'regexp' => 'Regexp',
            'min_length' => 'Min Length',
            'max_length' => 'Max Length',
            'is_required' => 'Is Required',
            'is_for_all' => 'Is For All',
            'is_filter' => 'Is Filter',
            'position' => 'Position',
            'is_searchable' => 'is_searchable',
            'default_value' => 'Default Value',
            'is_editable' => 'is_editable',
            'is_visible' => 'is_visible',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('id',$this->id);
        $criteria->compare('type',$this->type,true);
        $criteria->compare('name',$this->name,true);
        $criteria->compare('field_format',$this->field_format,true);
        $criteria->compare('possible_values',$this->possible_values,true);
        $criteria->compare('regexp',$this->regexp,true);
        $criteria->compare('min_length',$this->min_length);
        $criteria->compare('max_length',$this->max_length);
        $criteria->compare('is_required',$this->is_required);
        $criteria->compare('is_for_all',$this->is_for_all);
        $criteria->compare('is_filter',$this->is_filter);
        $criteria->compare('position',$this->position);
        $criteria->compare('is_searchable',$this->is_searchable);
        $criteria->compare('default_value',$this->default_value,true);
        $criteria->compare('is_editable',$this->is_editable);
        $criteria->compare('is_visible',$this->is_visible);

        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
        ));
    }
}